package thread;

public class ThreadDemo25 {
    public static void main(String[] args) {
        //需要一个统一的对象进行加锁,wait,notify
        Object locker = new Object();
        Thread t1 = new Thread(() ->{
           synchronized (locker) {
               System.out.println("t1 wait之前");
               try {
                   locker.wait();//wait必须放在sy里面
                   System.out.println("t1醒了");
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               System.out.println("t1 wait之后");
           }
        });
        Thread t2 = new Thread(() ->{
            try {
                Thread.sleep(2000);//必须写在sy外面不然t1,t2执行的顺序就不确定
                synchronized (locker) {
                    System.out.println("t2 notify之前");
                    locker.notify();//sleep之后在进行notify唤醒t1
                    //要确保代码先执行wait再执行notify
                    System.out.println("t2 notify之后");
                }
            } catch (InterruptedException e) {
                    e.printStackTrace();
            }
        });
        t1.start();
        t2.start();
    }
}
